package com.dgut.admodule.entity.dto;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.StringUtils;

import com.dgut.admodule.entity.Bidding;
import com.fasterxml.jackson.annotation.JsonFormat;

public class BiddingQueryDTO {
	private Long bidId;
	@DateTimeFormat(pattern="yyyy/MM/dd HH:mm:ss")
	private Date biddingbTime;
	@DateTimeFormat(pattern="yyyy/MM/dd HH:mm:ss")
	private Date biddingsTime;
	private Double minPrice;
	@DateTimeFormat(pattern="yyyy/MM/dd HH:mm:ss")
	private Date adsbTime;
	@DateTimeFormat(pattern="yyyy/MM/dd HH:mm:ss")
	private Date adssTime;
	
	public Long getBidId() {
		return bidId;
	}
	public void setBidId(Long bidId) {
		this.bidId = bidId;
	}
	@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss",timezone = "GMT+8")
	public Date getBiddingbTime() {
		return biddingbTime;
	}
	public void setBiddingbTime(Date biddingbTime) {
		this.biddingbTime = biddingbTime;
	}
	@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss",timezone = "GMT+8")
	public Date getBiddingsTime() {
		return biddingsTime;
	}
	public void setBiddingsTime(Date biddingsTime) {
		this.biddingsTime = biddingsTime;
	}
	public Double getMinPrice() {
		return minPrice;
	}
	public void setMinPrice(Double minPrice) {
		this.minPrice = minPrice;
	}
	@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss",timezone = "GMT+8")
	public Date getAdsbTime() {
		return adsbTime;
	}
	public void setAdsbTime(Date adsbTime) {
		this.adsbTime = adsbTime;
	}
	@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss",timezone = "GMT+8")
	public Date getAdssTime() {
		return adssTime;
	}
	public void setAdssTime(Date adssTime) {
		this.adssTime = adssTime;
	}
	
	public static Specification<Bidding> getSpecification(BiddingQueryDTO biddingQueryDTO)
	{
		Specification<Bidding> spec = new Specification<Bidding>() {
			public Predicate toPredicate(Root<Bidding> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//1.Predicate查询条件集合
				 List<Predicate> list = new ArrayList<Predicate>(); 
				 
				//2.根据 QueryDTO数据字段的值进行判断以及条件的组装
				 if(null != biddingQueryDTO && !StringUtils.isEmpty(biddingQueryDTO.getBidId())) {
					 Predicate  p1 =  cb.equal(root.get("bidId"),biddingQueryDTO.getBidId());
					 list.add(p1);
				 }
				 if(null != biddingQueryDTO && !StringUtils.isEmpty(biddingQueryDTO.getBiddingbTime())) {
					 Predicate  p2 =  cb.greaterThanOrEqualTo(root.get("biddingbTime"), biddingQueryDTO.getBiddingbTime());
					 list.add(p2);
				 }
				 if(null != biddingQueryDTO && !StringUtils.isEmpty(biddingQueryDTO.getAdsbTime())) {
					 Predicate  p3 =  cb.greaterThanOrEqualTo(root.get("adsbTime"), biddingQueryDTO.getAdsbTime());
					 list.add(p3);
				 }
				 
				 //3.Predicate查询条件集合的 size 创建对应的Predicate查询条件数组
				 Predicate[] p = new Predicate[list.size()];  
				 //4.CriteriaBuilder的and 函数组装 查询条件数组
				 return cb.and(list.toArray(p));  
			}
		};
		return spec;
	}
}
